import 'package:flutter/material.dart';
import 'package:ymt/ui/main_page.dart';
import 'package:ymt/utils/nav.dart';

class LoginForm extends StatefulWidget {
  @override
  _LoginFormState createState() => new _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  TextEditingController _unameController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  GlobalKey _formKey= new GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Container(child: Form(
      key: _formKey, //设置globalKey，用于后面获取FormState
      autovalidate: true, //开启自动校验
      child: Column(
        children: <Widget>[
          TextFormField(
              autofocus: true,
              controller: _unameController,
              decoration: InputDecoration(
                  hintText: "用户名",
                  icon: Icon(Icons.person)
              ),
              // 校验用户名
              validator: (v) {
                return "1de67b".compareTo(v.trim()) == 0 ? null : "用户名不正确";
              }

          ),
          TextFormField(
              controller: _pwdController,
              decoration: InputDecoration(
                  hintText: "您的登录密码",
                  icon: Icon(Icons.lock)
              ),
              obscureText: true,
            validator: (v){
              return "cffdff".compareTo(v.trim()) == 0 ? null : "密码不正确";
            },
          ),
          // 登录按钮
          Padding(
            padding: const EdgeInsets.only(top: 28.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: RaisedButton(
                    padding: EdgeInsets.all(15.0),
                    child: Text("登录"),
                    color: Theme
                        .of(context)
                        .primaryColor,
                    textColor: Colors.white,
                    onPressed: () {
                      //在这里不能通过此方式获取FormState，context不对
                      //print(Form.of(context));

                      // 通过_formKey.currentState 获取FormState后，
                      // 调用validate()方法校验用户名密码是否合法，校验
                      // 通过后再提交数据。
                      if((_formKey.currentState as FormState).validate()){
                        //验证通过提交数据
                        if("1de67b".compareTo(_unameController.text) == 0){
                          if("cffdff".compareTo(_pwdController.text) == 0){
                            Nav.destroyNav(context, MainPage(3433, "易卖通", "c23e4dd5e2cffdff"));
                          }
                        }
                      }
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    ),alignment: Alignment.center,width: 200,height: 300,);
  }
}